﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LabManager.UserControls.Tabs.Templates;
using DataConnector;
using Library;
using Equin.ApplicationFramework;
using System.Data.Entity;

namespace LabManager.UserControls.Tabs.Masterdata
{
    public partial class TabUserGroups : TabTemplate
    {
        private BindingListView<UserGroup> bindingList;
        private BindingSource bindingSource = new BindingSource();
        private int selectedID = -1;

        public TabUserGroups()
        {
            InitializeComponent();
        }

        public override void Load()
        {
            SetTexts();
            LoadUserGroupList();
        }

        private void SetTexts()
        {
            //TODO 
        }

        private void LoadUserGroupList()
        {
            bindingList = new BindingListView<UserGroup>(DataConnectorLayer.Instance.R_UserGroup.FindAll(x => x.UserGroupID > 0).ToList<UserGroup>());
            bindingSource.DataSource = bindingList;
            dgMain.DataSource = bindingSource;
            bnNavi.BindingSource = bindingSource;

            if (dgMain.Rows.Count > 0)
            {
                selectedID = (int)dgMain.Rows[0].Cells[0].Value;
            }

            dgMain_SelectionChanged(null, null);
        }

        #region Events

        private void dgMain_SelectionChanged(object sender, EventArgs e)
        {
            if (dgMain.SelectedRows.Count > 0)
            {
                var selectedRow = dgMain.SelectedRows[0];

                selectedID = (int)selectedRow.Cells[0].Value;
                tbGroupname.Text = selectedRow.Cells[1].Value.ToString();

                // Delete button
                var canDelete = DataConnectorLayer.Instance.R_User.FindOne(user => user.UserGroup.UserGroupID == selectedID) == null;
                btDelete.Enabled = canDelete;
                btUpdate.Enabled = true;
            }
            else
            {
                tbGroupname.Text = string.Empty;

                btUpdate.Enabled = false;
                btDelete.Enabled = false;
            }
        }

        private void btNew_Click(object sender, EventArgs e)
        {
            var item = new UserGroup() { UserGroupName = Helper.EMPTY_STRING };
            DataConnectorLayer.Instance.R_UserGroup.Create(item);
            DataConnectorLayer.Instance.R_UserGroup.SaveChanges();

            LoadUserGroupList();
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            var item = DataConnectorLayer.Instance.R_UserGroup.FindById(selectedID);
            item.UserGroupName = tbGroupname.Text != "" ? tbGroupname.Text : Helper.EMPTY_STRING;
            DataConnectorLayer.Instance.R_UserGroup.Update(item);
            DataConnectorLayer.Instance.R_UserGroup.SaveChanges();

            LoadUserGroupList();
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            var item = DataConnectorLayer.Instance.R_UserGroup.FindById(selectedID);
            DataConnectorLayer.Instance.R_UserGroup.Delete(item);
            DataConnectorLayer.Instance.R_UserGroup.SaveChanges();

            LoadUserGroupList();
        }

        private void tbFilter_TextChanged(object sender, EventArgs e)
        {
            var search = tbFilter.Text.ToLower();

            bindingList.ApplyFilter(delegate(UserGroup usergroup)
            {
                return usergroup.UserGroupName.ToLower().Contains(search);
            });
        }

        #endregion

    }
}